package com.example.administrator.xmut.http;

/**
 * Created by Administrator on 2016/2/12.
 */

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

public class login extends AppCompatActivity {
	private static final String ACTIVITY_TAG="LogDemo";
	private static AsyncHttpClient client;
	private PersistentCookieStore cookie;
	public static String jwxturl;
	public static String name="1407012215";
	public static String password="TamoQin0701";
	public static void loginAsyncHttpClientPost() {
		// 发送请求到服务器
		AsyncHttpClient client = new AsyncHttpClient();
		String url = "http://my.xmut.edu.cn/userPasswordValidate.portal";
		// 创建请求参数
		RequestParams params = new RequestParams();
		params.put("Login.Token1", "1407012215");
		params.put("Login.Token2", "TamoQin0701");
		params.put("goto", "http://my.xmut.edu.cn/loginSuccess.portal");
		params.put("gotoOnfail", "http://my.xmut.edu.cn/loginFailure.portal");
		// 执行post方法

		client.post(url, params, new AsyncHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode, Header[] headers,byte[] responseBody) {
				loginByAsyncHttpClientGet();

			}

			@Override
			public void onFailure(int statusCode, Header[] headers,byte[] responseBody, Throwable error) {
				// 打印错误信息
				error.printStackTrace();

			}
		});
	}

	/**
	 * 通过开源框架AsyncHttpClient的get请求处理
	 */
	private static void loginByAsyncHttpClientGet() {
		// 创建异步请求端对象
		AsyncHttpClient client = new AsyncHttpClient();

		String url = "http://my.xmut.edu.cn/index.portal";
		// 发送get请求对象
		client.get(url, new AsyncHttpResponseHandler() {
			@Override
			// statusCode:状态返回码，headers:头部请求信息，responseBody返回结果
			public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
								  byte[] responseBody, Throwable error) {
				// 输出错误信息
				error.printStackTrace();
			}
		});
	}

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initCookie(this);// cookie初始化
	}
	private void initCookie(Context context) {
		//必须在请求前初始化
		cookie = new PersistentCookieStore(context);
		getClient().setCookieStore(cookie);
		Log.e(login.ACTIVITY_TAG, "dddddddddddddddddddddddddddddddddd");
	}
	public static AsyncHttpClient getClient() {
		return client;
	}
}
